package com.lyhengtongwl.zqsnews.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import butterknife.BindView;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.SwipeMenuListAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.BaseEntity;
import com.lyhengtongwl.zqsnews.entity.MyaddrEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.*;
import retrofit2.Response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * rv_myaddr
 * 收货地址
 */
public class MyaddrActivity extends BaseActivity {

    @BindView(R.id.mSwipeMenuListView)
    SwipeMenuListView mSwipeMenuListView;
    private List<MyaddrEntity> mList = new ArrayList<>();
    private SwipeMenuListAdapter menuListAdapter;
    private String name;
    private String phone;
    private String address;
    private String addressId;
    private boolean isChange = false;

    @Override
    public void setListen() {


    }

    @Override
    public void setTitleBarColor() {

    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    private void setSwipeMenuListView() {
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem swipeMenuItem = new SwipeMenuItem(App.getContext());
                swipeMenuItem.setBackground(R.color.mainBg);
                //swipeMenuItem.setTitleColor(R.color.white);
                swipeMenuItem.setTitleColor(Color.WHITE);
                swipeMenuItem.setWidth(dp2px(90));
                swipeMenuItem.setTitle("删除");
                swipeMenuItem.setTitleSize(14);
                menu.addMenuItem(swipeMenuItem);
            }
        };
        mSwipeMenuListView.setMenuCreator(swipeMenuCreator);
        mSwipeMenuListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
        //设置监听
        mSwipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        removeAddr(mList.get(position).getAddressId());
                        mList.remove(position);
                        menuListAdapter.notifyDataSetChanged();
                        break;
                }
                return false;
            }
        });
        mSwipeMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //查看申请进度
                if (isChange) {
                    name = mList.get(position).getReceiver();
                    phone = mList.get(position).getMobile();
                    address = mList.get(position).getAddress();
                    addressId = mList.get(position).getAddressId();
                    startActivity(new Intent(MyaddrActivity.this, ConfirmOrderActivity.class)
                            .putExtra("name", name)
                            .putExtra("phone", phone)
                            .putExtra("address", address)
                            .putExtra("addressId", addressId));
                    finish();
                }
            }
        });
        // set SwipeListener
        mSwipeMenuListView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

            @Override
            public void onSwipeStart(int position) {
                // swipe start
            }

            @Override
            public void onSwipeEnd(int position) {
                // swipe end
            }
        });

        // set MenuStateChangeListener
        mSwipeMenuListView.setOnMenuStateChangeListener(new SwipeMenuListView.OnMenuStateChangeListener() {
            @Override
            public void onMenuOpen(int position) {
            }

            @Override
            public void onMenuClose(int position) {
            }
        });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_myaddr;
    }

    @Override
    public void initView() {
        setSwipeMenuListView();//设置 滑动删除
        new TitleBuilder(this).setTitleText("收货地址")
                .setRightText("添加新地址")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                }).setRightTextListening(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ScreenUtil.isFastClick()) {
                    return;
                }
                startActivityForResult(new Intent(MyaddrActivity.this, AddaddrActivity.class), 1);
            }
        });
    }

    @Override
    public void initData() {
        isChange = getIntent().getBooleanExtra("change", false);
        if (menuListAdapter == null) {
            menuListAdapter = new SwipeMenuListAdapter(this, LayoutInflater.from(this), mList);
        }
        mSwipeMenuListView.setAdapter(menuListAdapter);
        getMyAddrList();
    }

    /**
     * 获取我的地址列表
     */
    private void getMyAddrList() {
        //获取我的地址列表
        Task.getApiService().getMyAddrList().enqueue(new MyCallback<BaseEntity>(MyaddrActivity.this) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                try {
                    SaveParameterUtils.saveToken(response.raw().header("token"));
                    if ("0".equals(response.body().getCode())) {
                        List<MyaddrEntity> addrList = GsonUtils.jsonToArrayList(
                                response.body().getResult().getJSONArray("addressList").toString(),
                                MyaddrEntity.class);
                        if (addrList != null) {
                            mList.clear();
                            mList.addAll(addrList);
                            Collections.sort(mList, new Comparator<MyaddrEntity>() {

                                @Override
                                public int compare(MyaddrEntity o1, MyaddrEntity o2) {
                                    if (o1.getStatus().contains("1")) {
                                        return -1;
                                    }
                                    return 1;
                                }
                            });
                            menuListAdapter.notifyDataSetChanged();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            getMyAddrList();
        }

    }

    /**
     * 删除地址
     */
    private void removeAddr(String addressId) {
        Task.getApiService().removeAddr(addressId).enqueue(new MyCallback<BaseEntity>(this) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                try {
                    ToastUtil.showShort(MyaddrActivity.this,
                            response.body().getMessage());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {
            }
        });
    }


}
